import { mapGetters } from 'vuex'
import apiCash from '@/common/api/apiCash'
import store from '@/store'
export default {
  data() {
    return {
      token: '',
    }
  },
  computed: {
    ...mapGetters(['userInfo','selectUserInfo']),
  },
  created() {},
  methods: {
    // 选择用户
    selectUser(userInfo) {
      this.$refs.actionSheet.hideSheet();
      store.dispatch("user/setSelectUserInfo", userInfo);
      if (!this.token) {
        this.getToken();
        return;
      }
      this.getExchangeList(userInfo);
    },
    // 兑换现金
    getExchangeAmountToken(){
      this.$toast.loading()
      apiCash.getTokenInfo({ userId: this.userInfo.userId }).then((data) => {
        let token = data.data.token || ''
        this.token = token
        this.$toast.clear();
        if (token) {
          this.exchangeAmount()
        }
      })
    },
    // 兑换现金
    exchangeAmount(){
      this.$toast.loading();
      apiCash
        .exchangeCash({ userId: this.userInfo.userId, amount: Number(this.amount),token:this.token,cashCouponId:this.exchangeInfo.cashCouponId,weilaiUserId:this.selectUserInfo.userId })
        .then(() => {
          this.$toast.clear();
          this.$refs.dialog.showDialog();
        });
    },
    // 兑换洗衣券
    getExchangeCouponToken(){
      this.$toast.loading()
      apiCash.getTokenInfo({ userId: this.userInfo.userId }).then((data) => {
        let token = data.data.token || ''
        this.token = token
        this.$toast.clear();
        if (token) {
          this.exchangeCoupon()
        }
      })
    },
    // 兑换洗衣券
    exchangeCoupon(){
      this.$toast.loading();
      apiCash
        .exchangeCoupon({ userId: this.userInfo.userId, token:this.token,cashCouponId:this.exchangeInfo.cashCouponId,couponId:this.couponInfo.id,weilaiUserId:this.selectUserInfo.userId })
        .then(() => {
          this.$toast.clear();
          this.$refs.dialog.showDialog();
        });
    },
    // 获取token
    getToken() {
      this.$toast.loading()
      apiCash.getTokenInfo({ userId: this.userInfo.userId }).then((data) => {
        let token = data.data.token || ''
        this.token = token
        if (token) {
          this.getExchangeList()
        }
      })
    },
    //  获取可兑换信息
    getExchangeList() {
      this.$toast.loading()
      apiCash
        .getExchangeList({
          userId: this.selectUserInfo.userId,
          token: this.token,
        })
        .then((data) => {
          this.$toast.clear()
          // 0  没有兑换内容 1  兑换优惠券 2  兑换蔚来币 3  都可以
          if (data.data.exchangeType === '0') {
            this.$toast.err('无可兑换产品')
          }
          if (data.data.exchangeType === '1') {
            this.$router.push({
              path: '/exchange',
              query: {
                exchangeList: JSON.stringify(data.data.coupleSources),
                exchangeInfo: JSON.stringify(this.currCashInfo),
              },
            })
          }
          if (data.data.exchangeType === '2') {
            this.$router.push({
              path: '/exchangeCenter',
              query: {
                exchangeInfo: JSON.stringify(this.currCashInfo),
              },
            })
          }
        })
    },
  },
}
